/*
 * #%L
 * Client
 * %%
 * Copyright (C) 2011 Ractoc.com
 * %%
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * #L%
 */
package com.ractoc.opengamefinder.client.screens.controllers;

import java.util.Properties;

import de.lessvoid.nifty.Nifty;
import de.lessvoid.nifty.controls.Controller;
import de.lessvoid.nifty.elements.Element;
import de.lessvoid.nifty.elements.render.ImageRenderer;
import de.lessvoid.nifty.elements.render.TextRenderer;
import de.lessvoid.nifty.input.NiftyInputEvent;
import de.lessvoid.nifty.render.NiftyImage;
import de.lessvoid.nifty.screen.Screen;
import de.lessvoid.xml.xpp3.Attributes;
import org.apache.log4j.Logger;

/**
 * Main controller for the message box.
 * @author ractoc
 * @version 0.1
 */
public final class MessageController implements Controller {

    private static Logger logger = Logger.getLogger(MessageController.class.getName());
    private Nifty nifty;
    private Element element;

    /**
     * Default constructor.
     */
    public MessageController() {
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void bind(final Nifty niftyParam, final Screen screenParam, final Element newElement,
            final Properties properties, final Attributes controlDefinitionAttributes) {
        nifty = niftyParam;
        element = newElement;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public boolean inputEvent(final NiftyInputEvent event) {
        return true;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void onFocus(final boolean focus) {
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void onStartScreen() {
    }

    /**
     * Set the supplied text to be used in the message box.
     * @param text The supplied text.
     */
    public void setMessageText(final String text) {
        logger.debug("adding " + text + " to element " + element.getId());
        element.findElementByName("#message-text").getRenderer(TextRenderer.class).setText(text);
    }

    /**
     * Set the supplied icon to be used in the message box.
     * @param icon The supplied icon, this is the complete filename for the icon image.
     */
    public void setMessageIcon(final String icon) {
        try {
            logger.debug("adding " + icon + " to element " + element.getId());
            final NiftyImage newImage = nifty.createImage(icon, false);
            final Element iconElement = element.findElementByName("#message-icon");
            logger.debug(iconElement);
            logger.debug(iconElement.getRenderer(ImageRenderer.class));
            element.findElementByName("#message-icon").getRenderer(ImageRenderer.class).setImage(newImage);
        }
        catch (RuntimeException e) {
            logger.error("Unable to create icon: " + icon, e);
        }
    }

    /**
     * Show the actual message box.
     * @param text The text to show.
     * @param icon The icon to show, this is the complete filename for the icon image.
     */
    public void showMessage(final String text, final String icon) {
        logger.debug(icon + " = " + text);
        setMessageText(text);
        setMessageIcon(icon);
    }

    /**
     * Stop displaying the message box.
     */
    public void hideMessage() {
        setMessageText("");
        nifty.closePopup(element.getId());
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void init(final Properties parameter, final Attributes controlDefinitionAttributes) {
    }
}
